package org.nonsense;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javaFlacEncoder.StreamConfiguration;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFileFormat.Type;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.TargetDataLine;

class CaptureThread extends Thread	{
	//An arbitrary-size temporary holding buffer
	private RecorderModel recorderModel;
	private boolean stopCapture;
	private ByteArrayOutputStream byteArrayOutputStream;
	private TargetDataLine targetDataLine;
	private MyFlacEncoder mfe;
	private AudioInputStream audioInputStream;
	
	public CaptureThread(RecorderModel rm)	{
		recorderModel = rm;
		targetDataLine = rm.getTargetDataLine();
	}
	
	public void stopCapture()	{
		System.out.println("Stopping Capture...");
		stopCapture = true;
		targetDataLine.stop();
		targetDataLine.close();
		mfe.setFinished(true);
	}
	
	public void run()	{
		mfe = new MyFlacEncoder();
		File outputFile;
		
		
		
		byteArrayOutputStream = new ByteArrayOutputStream();
		stopCapture = false;
		
		try	{
			System.out.println("Beginning Capture...");
			targetDataLine.open(recorderModel.getAudioFormat());
			targetDataLine.start();
			
			// this call will block until we finish!
			audioInputStream = new AudioInputStream(targetDataLine);
			
			String fileName = recorderModel.getArchiveDirectory() + "\\" + recorderModel.getOutputFilePrefix();
			
			switch(recorderModel.getOutputFormat())	{
			case 0:	// FLAC
				System.out.println("Encoding: FLAC");
				outputFile = new File(fileName+".flac");
				mfe.encodeStream(audioInputStream, outputFile);
				break;
			case 1: // WAV
				System.out.println("Encoding: WAV");
				outputFile = new File(fileName+".wav");
				AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, outputFile);
				break;
			case 2: // AU
				System.out.println("Encoding: AU");
				outputFile = new File(fileName+".au");
				AudioSystem.write(audioInputStream, AudioFileFormat.Type.AU, outputFile);
				break;
			case 3: // AIFF
				System.out.println("Encoding: AIFF");
				outputFile = new File(fileName+".aiff");
				AudioSystem.write(audioInputStream, AudioFileFormat.Type.AIFF, outputFile);
				break;
			case 4: 
				System.out.println("Encoding: AIFC");
				outputFile = new File(fileName+".aifc");
				AudioSystem.write(audioInputStream, AudioFileFormat.Type.AIFC, outputFile);
				break;
			case 5:
				System.out.println("Encoding: SND");
				outputFile = new File(fileName+".snd");
				AudioSystem.write(audioInputStream, AudioFileFormat.Type.SND, outputFile);
				break;
			default:
				System.err.println("Invalid output type selected");
			}
			
			System.out.println("Capture Complete.");
			audioInputStream.close();
			recorderModel.captureComplete();
			
			System.out.println("Capture Thread Exiting.");
			
		}
		catch (Exception e) {
			System.out.println(e);
			System.exit(0);
		}
	}
}

